﻿// <copyright file="Responses.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Collections.Generic;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "responses", Namespace = "http://search.yahoo.com/mrss/")]
    public class Responses : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "responses";

        #endregion

        #region Public Properties

        public List<Response> ResponseItems { get; set; }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            base.AddAttributeExtensions(reader);
            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                while (reader.IsStartElement())
                {
                    if (reader.LocalName == "response")
                    {
                        if (this.ResponseItems == null)
                        {
                            this.ResponseItems = new List<Response>();
                        }
                        var item = new Response();
                        item.ReadXml(reader);
                        this.ResponseItems.Add(item);
                    }
                    else
                    {
                        base.ElementExtensions.Add((XElement)XNode.ReadFrom(reader));
                    }
                }
            }
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            if ((this.ResponseItems != null) && (this.ResponseItems.Count > 0))
            {
                foreach (var response in this.ResponseItems)
                {
                    response.WriteXml(writer);
                }
            }
            foreach (var element in base.ElementExtensions)
            {
                element.WriteTo(writer);
            }
        }

        #endregion
    }
}